/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Parser for search arguments
 */

package Utilities;

import java.util.StringTokenizer;
import java.util.Vector;
import java.util.regex.*;

public class Parser {
	private static String[] ShouldBeParsed = {
		"di","ke","dari","untuk","guna","hingga","hampir","demi","atas",
		//Insert conjunction here
                "agar", "apabila", "atau", "bahwa", "dan", "jika", "jikalau",
                "kecuali", "karena", "lalu", "manakala", "sambil", "serta", "semenjak",
                "sementara", "sungguhpun", "supaya", "walaupun", "tetapi", "yang", 
		//and insert more words again
	};

	//Parse arguments
	public static Vector<String> ParseText(String args) {
		Vector<String> TempArguments = null;

		//Turn all to lowercase
		String TempArgs = args.toLowerCase();

		//Remove all punctuations
        Pattern p = Pattern.compile("[\\p{Punct}]");
        Matcher m = p.matcher(TempArgs);

        StringBuffer sb = new StringBuffer();
        boolean result = m.find();

        while(result) {
            m.appendReplacement(sb, "");
            result = m.find();
        }
        m.appendTail(sb);
        TempArgs = sb.toString();

        //Remove words that should be parsed
        for(int i=0;i<ShouldBeParsed.length;++i) {
        	TempArgs = TempArgs.replace(ShouldBeParsed[i], "");
        }

        //Tokenize string
        StringTokenizer st = new StringTokenizer(TempArgs);
        if (st.hasMoreTokens()) TempArguments = new Vector<String>();
        while(st.hasMoreTokens()) TempArguments.add(st.nextToken());

		return TempArguments;
	}
}
